package ecoiot.com.testproject.hencoder;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.OvershootInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import ecoiot.com.testproject.R;
import ecoiot.com.testproject.utils.Utils;

/**
 * Created by developer on 17-12-7.
 */

@SuppressLint("NewApi")
public class ThumbUpView extends View implements View.OnClickListener{

    private Context mContext;
    private Bitmap mBitmapShining;
    private Bitmap mBitmapSelected;
    private Bitmap mBitmapUnSelected;
    private Paint mPaint;

    private int mDefaultCount = 313;

    private float mUnSelectScale = 0;
    private float mSelectScale = 0;
    private float mShiningScale = 0;

    private boolean select = false;


    public ThumbUpView(Context context) {
        super(context);
        mContext = context;
    }

    public ThumbUpView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initData();
    }

    private void initData() {
        mPaint = new Paint();
        mBitmapUnSelected = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_messages_like_unselected);
        mBitmapShining = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_messages_like_selected_shining);
        mBitmapSelected = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_messages_like_selected);
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (select) {
            canvas.save();
            canvas.drawBitmap(mBitmapShining,100,100,mPaint);
            canvas.restore();
            canvas.drawBitmap(mBitmapSelected,97,117,mPaint);
        }else {
            canvas.drawBitmap(mBitmapUnSelected,97,117,mPaint);
        }

        mPaint.setTextSize(Utils.sp2px(15,getContext()));
        mPaint.setColor(Color.parseColor("#cccccc"));
        canvas.drawText(String.valueOf(mDefaultCount),145,145,mPaint);

    }


    public void setUnselectedScale(float scale) {
        mUnSelectScale = scale;
        Matrix matrix = new Matrix();
        matrix.postScale(mUnSelectScale, mUnSelectScale);

        mBitmapShining = Bitmap.createBitmap(mBitmapShining,0,0,mBitmapShining.getWidth(),mBitmapShining.getHeight(),matrix,true);

        postInvalidate();
    }

    public float getUnselectedScale() {
        return mUnSelectScale;
    }


    @Override
    public void onClick(View v) {
        ObjectAnimator thumbUpScale = ObjectAnimator.ofFloat(this, "unselectedScale", 0.9f, 1.0f);
        thumbUpScale.setDuration(150);
        thumbUpScale.setInterpolator(new OvershootInterpolator());

        AnimatorSet set = new AnimatorSet();
        set.play(thumbUpScale);

        select = !select;
    }
}
